package com.example.springboot.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springboot.config.Shiro.security.SecurityUtil;
import com.example.springboot.mapper.EnshrineMapper;
import com.example.springboot.mapper.MaterialMapper;
import com.example.springboot.mapper.WallHavenMapper;
import com.example.springboot.model.entity.Enshrine;
import com.example.springboot.model.entity.Material;
import com.example.springboot.model.entity.WallHaven;
import com.example.springboot.model.form.Result;
import com.example.springboot.model.req.admin.WallHavenReq;
import com.example.springboot.model.sys.SysUser;
import com.example.springboot.model.vo.WallpaperVO;
import com.example.springboot.service.admin.WallHavenService;
import com.example.springboot.utils.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

@Service
@Slf4j
public class IndexServiceImpl implements IndexService {

    @Resource
    WallHavenMapper wallHavenMapper;

    @Resource
    MaterialMapper materialMapper;

    @Resource
    EnshrineMapper enshrineMapper;

    @Resource
    WallHavenService wallHavenService;

    @Override
    public Result getRandomImg() {
        WallpaperVO wallpaperVO = new WallpaperVO();
        //随机壁纸
        WallHaven wallHaven = wallHavenMapper.selectOne(new LambdaQueryWrapper<WallHaven>()
                .eq(WallHaven::getInvalid, 0)
                .orderByDesc(WallHaven::getId)
                .last("limit 1"));
        while (true) {
            int number = new Random().nextInt(wallHaven.getId() + 1);
            WallHaven byId = wallHavenMapper.selectById(number);
            if (byId != null) {
                CopyUtil.copyObject(byId, wallpaperVO);
                SysUser sysUser = SecurityUtil.sysUser();
                if (Objects.nonNull(sysUser)) {
                    //收藏
                    Enshrine enshrine = enshrineMapper.selectOne(new LambdaQueryWrapper<Enshrine>()
                            .eq(Enshrine::getInvalid, 0)
                            .eq(Enshrine::getUserId, sysUser.getId())
                            .eq(Enshrine::getEnshrineId, byId.getId()));
                    if (Objects.nonNull(enshrine)) {
                        wallpaperVO.setIsEnshrine(1);
                    }
                }
                break;
            }
        }

        //随机素材
        Material material = materialMapper.selectOne(new LambdaQueryWrapper<Material>()
                .eq(Material::getInvalid, 0)
                .orderByDesc(Material::getId)
                .last("limit 1"));
        while (true) {
            int num = new Random().nextInt(material.getId() + 1);
            Material getMaterial = materialMapper.selectById(num);
            if (getMaterial != null) {
                wallpaperVO.setContentId(getMaterial.getId());
                wallpaperVO.setContent(getMaterial.getContent());
                wallpaperVO.setSource(getMaterial.getSource());
                wallpaperVO.setAuthor(getMaterial.getAuthor());
                break;
            }
        }
        return Result.buildSuccess(wallpaperVO);
    }

    @Override
    public Result getTenRandomImg(WallHavenReq req) {
        SysUser sysUser = SecurityUtil.sysUser();
        if (Objects.isNull(sysUser)) {
            List<WallpaperVO> wallpaperVO = new ArrayList();
            for (int i = 0; i < 10; i++) {
                wallpaperVO.add((WallpaperVO)getRandomImg().getModel());
            }
            return Result.buildSuccess(wallpaperVO);
        }else {
            return wallHavenService.getPage(req);
        }
    }

}
